package org.usp.poli.bipt.client.ui;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FocusWidget;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import org.usp.poli.bipt.client.*;

public class Menu {
	private String imagem, texto;
	private PushButton btnMenu; 

	private Buttons newresourceBtn;
	private Buttons explorerBtn;
	private Buttons homeBtn;
	private Buttons loginBtn;
	private Buttons logoutBtn;

	final Login loginDialog = new Login();
	
	private HorizontalPanel menuBtn = new HorizontalPanel();
	private VerticalPanel menuForm = new VerticalPanel();
	public Menu() {
		//--Menu de bot�es
	    //menuBtn.addStyleName("menuBtn");
	    
		//menuBtnLogout();
		
		//menuForm();
	}



	private void menuBtnLogout() {
		menuBtn.clear();
		menuBtn.setSpacing(15);
		final PopupPanel textoDescritivo= new PopupPanel();
		setHomeBtn(new Buttons("home", "Home", textoDescritivo));
		setExplorerBtn(new Buttons("find_resource", "Find Resource", textoDescritivo));
		setLoginBtn(new Buttons("login", "Login", textoDescritivo));
		menuBtn.add(getHomeBtn().getBotao());
		menuBtn.add(getExplorerBtn().getBotao());
		menuBtn.add(getLoginBtn().getBotao());
		menuBtn.setHorizontalAlignment(HorizontalPanel.ALIGN_CENTER);
	    
		((PushButton) this.getLoginBtn().getBotao()).addClickHandler(new ClickHandler() {
	    	public void onClick(ClickEvent sender) {
	    		//BrazilianIPT.mainareaVPnl.clear();

				((DialogBox) loginDialog.getLogin()).center();
				((DialogBox) loginDialog.getLogin()).show();
	    		
	    		
	    	}
	    });
		
		((Button)loginDialog.getBotaoCancelar()).addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				((DialogBox) loginDialog.getLogin()).hide();

			}
		});
		
		((PushButton) this.getHomeBtn().getBotao()).addClickHandler(new ClickHandler() {
	    	public void onClick(ClickEvent sender) {
	    		BrazilianIPT.setHome();
	    	}
	    });
		
		((Button)loginDialog.getBotaoOk()).addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				((DialogBox) loginDialog.getLogin()).hide();
				BrazilianIPT.greetingService.login(loginDialog.getUserLogin(), loginDialog.getUserSenha() ,
						new AsyncCallback<Boolean>() {
					public void onFailure(Throwable caught) {
						
					}

					
					public void onSuccess(Boolean result) {
						if(result.equals(true)){
							BrazilianIPT.setMenuLogin();
						}
						else{
							
							
						}
						
					}
				});

			}
		});
		
		
		
		
	}
	
	private void menuBtnLogin() {
		menuBtn.clear();
		menuBtn.setSpacing(15);
		final PopupPanel textoDescritivo= new PopupPanel();
		setHomeBtn(new Buttons("home", "Home", textoDescritivo));
		setNewresourceBtn(new Buttons("new_resource", "New Resource", textoDescritivo));
		setExplorerBtn(new Buttons("find_resource", "Find Resource", textoDescritivo));
		setLogoutBtn(new Buttons("logout", "Logout", textoDescritivo));
		menuBtn.add(getHomeBtn().getBotao());
		menuBtn.add(getNewresourceBtn().getBotao());
		menuBtn.add(getExplorerBtn().getBotao());
		menuBtn.add(getLogoutBtn().getBotao());
		menuBtn.setHorizontalAlignment(HorizontalPanel.ALIGN_CENTER);
	    
		((PushButton) this.getLogoutBtn().getBotao()).addClickHandler(new ClickHandler() {
	    	public void onClick(ClickEvent sender) {
	    		BrazilianIPT.setMenuLogout();
	    		
	    		
	    	}
	    });
		((PushButton) this.getNewresourceBtn().getBotao()).addClickHandler(new ClickHandler() {
	    	public void onClick(ClickEvent sender) {
	    		BrazilianIPT.setResourceConfig();
	    	}
	    });
		((PushButton) this.getHomeBtn().getBotao()).addClickHandler(new ClickHandler() {
	    	public void onClick(ClickEvent sender) {
	    		BrazilianIPT.setHome();
	    	}
	    });
		
		
		
	}

	public HorizontalPanel getMenuLogout(){
		this.menuBtnLogout();
		
		return this.menuBtn;
	}
	
	public HorizontalPanel getMenuLogin(){

		this.menuBtnLogin();
		return this.menuBtn;
	}
	
	
	public VerticalPanel getMenuForm(){

		
		return this.menuForm;
	}

	private void setNewresourceBtn(Buttons newresourceBtn) {
		this.newresourceBtn = newresourceBtn;
	}

	public Buttons getNewresourceBtn() {
		
		return newresourceBtn;
	}

	public Buttons getFindResourcesBtn() {
		return getExplorerBtn();
	}


	private void setExplorerBtn(Buttons explorerBtn) {
		this.explorerBtn = explorerBtn;
	}



	private Buttons getExplorerBtn() {
		return explorerBtn;
	}



	private void setHomeBtn(Buttons homeBtn) {
		this.homeBtn = homeBtn;
	}



	private Buttons getHomeBtn() {
		return homeBtn;
	}



	private void setLoginBtn(Buttons loginBtn) {
		this.loginBtn = loginBtn;
	}



	public Buttons getLoginBtn() {
		
		return loginBtn;
	}
	

	private void setLogoutBtn(Buttons logoutBtn) {
		this.logoutBtn = logoutBtn;
	}



	public Buttons getLogoutBtn() {
		
		
		return logoutBtn;
	}

}
